package com.tz.springmvc.sysmanage.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.tz.springmvc.sysmanage.entity.User;

/**
 * 用于处理文件上传的contoller
 * @author Administrator
 *
 */
@Controller
@RequestMapping("/sysmanage/test")
public class SpecialFormatterController {
	 
	//进入specialFormatter的页面
	@RequestMapping("/gotoSpecialFormatter")
	public String gotoSpecialFormatter(){
		return "sysmanage/test/specialFormatter";
	}
	 
	//处理特殊格式
	@RequestMapping("/testSpecialFormatter")
	public String testSpecialFormatter(@RequestParam("specialFormatter") User user){
		System.out.println(user.getUserName());
		return "sysmanage/test/specialFormatter";
	}
	
	//处理特殊格式
	@RequestMapping("/testDateFormatter")
	public String testDateFormatter(@Validated User user ,BindingResult bindingResult,
			Model model){
		//int i = 1/0;
		User userNUllTest = null;
		userNUllTest.getAge();
		System.out.println(user.getUserName());
		System.out.println(user.getBirthday());
		//如果校验时有不符合校验规则情况的出现,他会讲校验的提示信息放在bindingResult的 errors对象里面
		if(bindingResult.hasErrors()){
			List<ObjectError> errors =bindingResult.getAllErrors();
			model.addAttribute("errors", errors);
			for(ObjectError error:errors){
				System.out.println(error.getDefaultMessage());
			}
		}	
		return "sysmanage/test/specialFormatter";
	}
	
	/*@ExceptionHandler({Exception.class})
	public String exp1(HttpServletRequest request,Exception ex){
		request.setAttribute("ex", ex);
		return "errors/error_exceptionHandlerAnnotation";
	}*/
	
	@ExceptionHandler({ArithmeticException.class})
	public String exp2(HttpServletRequest request,Exception ex){
		request.setAttribute("ex", ex);
		return "errors/error_exceptionHandlerAnnotation";
	}

}
